import Vue from 'vue'
import VueRouter from 'vue-router'
import config from './config'
// 引入nprogress
import NProgress from 'nprogress' // 进度条
import 'nprogress/nprogress.css' // 这个样式必须引入

Vue.use(VueRouter)

// 简单配置
NProgress.inc(0.2)
NProgress.configure({ easing: 'ease', speed: 300, showSpinner: false })

const routes = config.map(i => {
    const obj = {
        path: i.path
    }
    if (i.redirect) {
        obj.redirect = i.redirect
    } else {
        obj.component = () => import('@/views/' + i.component + '.vue')
    }
    if (i.name) obj.name = i.name
    if (i.meta) obj.meta = i.meta
    return obj
})

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    NProgress.start()
    if (to?.meta?.title) document.title = to.meta.title
    // if (!localStorage.getItem('token')) { // 没有登录 重定向登录页
    //     to.path === '/login' ? next() : next('/login')
    //     return
    // } else if (to.matched.length === 0) {
    //     next('/error')
    //     return
    // }
    next()
})

router.afterEach((to, from) => {
    NProgress.done()
})

export default router
